package club.janna.concurrency.example.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @Auther: guopanbo
 * @Date: 2018/7/21 23:41
 * @Description:
 */
@Slf4j
public class LockExample5 {




    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            lock.lock();
            try {
                log.info("get lock1");
                log.info("waiting signal");
                condition.await();
                log.info("get signal");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                log.info("get lock2");
                condition.signalAll();
                log.info("send signal");
            } finally {
                lock.unlock();
            }
        }).start();
    }
}

